import { ref } from 'vue'
import { defineStore } from 'pinia'

/* 交管事件相关状态 */
export const useTrafficStore = defineStore('traffic', () => {
  //事件坐标
  const eventPosition = ref([])
  //AddPopup状态
  const addPopupVisible = ref(false)
  //UpdatePopup状态
  const updatePopupVisible = ref(false)
  //noticeInformation状态
  const publicNoticeVisible = ref(false)
  //watchNotice状态
  const watchNoticeVisible = ref(false)
  //manageNotice状态
  const manageNoticeVisible = ref(false)
  // 切换 AddPopup 可见性
  const toggleAddPopupVisible = () => {
    addPopupVisible.value = !addPopupVisible.value
  }
  // 切换 UpdatePopup 可见性
  const toggleUpdatePopupVisible = () => {
    updatePopupVisible.value = !updatePopupVisible.value
  }
  // 切换 noticeInformation 可见性
  const togglePublicNoticeVisible = () => {
    publicNoticeVisible.value = !publicNoticeVisible.value
  }
  // 切换 watchNotice 可见性
  const toggleWatchNoticeVisible = () => {
    watchNoticeVisible.value = !watchNoticeVisible.value
  }
  //事件更新查询到的属性
  const fid = ref(null)
  const updateEventList = ref([])
  const timeLabel = ref('')
  //发布公告查询到的属性name,tel
  const publicNoticeList = ref([])
  // 发布公告相关的数据
  const noticeTitle = ref('')
  const noticeInfo = ref('')
  //画笔数据源
  let addPopupSource, updatePopupSource, publicNoticeSource;

  return { eventPosition, addPopupVisible, updatePopupVisible, publicNoticeVisible, watchNoticeVisible, manageNoticeVisible, fid, updateEventList, timeLabel, publicNoticeList, noticeTitle, noticeInfo, addPopupSource, updatePopupSource, publicNoticeSource, toggleAddPopupVisible, toggleUpdatePopupVisible, togglePublicNoticeVisible, toggleWatchNoticeVisible, }
})

/* 登录相关状态 */
export const useLoginStore = defineStore('login', () => {
  const userType = ref('common')
  const changeUserType = (newVal) => {
    userType.value = newVal
  }
  //当前登录用户信息
  const defaultCurrentUser = () => {
    let user = ref(null)
    let localUser = localStorage.getItem('currentUser')
    if (localUser) {
      user.value = JSON.parse(localUser)
    }
    return user
  }
  const currentUser = defaultCurrentUser()
  const setCurrentUser = (newVal) => {
    console.log('newVal', newVal)
    currentUser.value = newVal
    localStorage.setItem('currentUser', JSON.stringify(newVal))
    localStorage.setItem('currentUser', JSON.stringify(newVal))
  }


  return {
    userType,
    changeUserType,
    currentUser,
    setCurrentUser,
  }
})

/* 查看公告通知相关状态 */
export const useNoticeStore = defineStore('notice', () => {
  const dialogVisible = ref(false)
  const showDialog = () => {
    dialogVisible.value = true
  }
  return {
    dialogVisible,
    showDialog,
  }
})

/* 路况相关状态 */
export const useRoadConditoinStore = defineStore('roadCondition', () => {
  const trafficVisible = ref(false)
  const showTraffic = () => {
    trafficVisible.value = true
  }
  const roadConditionVisible = ref(false)
  const showRoadCondition = () => {
    roadConditionVisible.value = true
  }
  // 路况查询的坐标
  const roadConditionLocation = ref([])

  return {
    trafficVisible,
    showTraffic,
    roadConditionVisible,
    showRoadCondition,
    roadConditionLocation,
  }
})

// 控制导航栏所有功能的展开与关闭
export const useHeaderNavStore = defineStore('headerNav', () => {
  // 控制用户信息弹窗的可见性
  const dialogVisible = ref(false)

  const changeDialogVisible = (newVal) => {
    dialogVisible.value = newVal
  }
  return {
    dialogVisible,
    changeDialogVisible,
  }
})
